package com.imooc.controller;



import com.imooc.MinIOConfig;
import com.imooc.base.BaseInfoProperties;
import com.imooc.bo.UserInfoBo;
import com.imooc.enums.FileTypeEnum;
import com.imooc.enums.UserInfoModifyType;
import com.imooc.grace.result.GraceJSONResult;

import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.pojo.Users;
import com.imooc.service.UsersService;

import com.imooc.utils.MinIOUtils;
import com.imooc.vo.UsersVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@Slf4j
@RequestMapping("userInfo")
@RestController
@Api(tags = "用户信息")
public class UserInfoController extends BaseInfoProperties {

        @Autowired
        private UsersService usersService;
        @Autowired
        private MinIOConfig minIOConfig;

        @GetMapping("query")
        public GraceJSONResult query(@RequestParam String userId){
                Users user = usersService.getUser(userId);
                UsersVo usersVO = new UsersVo();
                BeanUtils.copyProperties(user, usersVO);

                // 我的关注博主总数量
                String myFollowsCountsStr = redis.get(REDIS_MY_FOLLOWS_COUNTS + ":" + userId);
                // 我的粉丝总数
                String myFansCountsStr = redis.get(REDIS_MY_FANS_COUNTS + ":" + userId);
                // 用户获赞总数，视频博主（点赞/喜欢）总和
//        String likedVlogCountsStr = redis.get(REDIS_VLOG_BE_LIKED_COUNTS + ":" + userId);
                String likedVlogerCountsStr = redis.get(REDIS_VLOGER_BE_LIKED_COUNTS + ":" + userId);

                Integer myFollowsCounts = 0;
                Integer myFansCounts = 0;
                Integer likedVlogCounts = 0;
                Integer likedVlogerCounts = 0;
                Integer totalLikeMeCounts = 0;

                if (StringUtils.isNotBlank(myFollowsCountsStr)) {
                        myFollowsCounts = Integer.valueOf(myFollowsCountsStr);
                }
                if (StringUtils.isNotBlank(myFansCountsStr)) {
                        myFansCounts = Integer.valueOf(myFansCountsStr);
                }
//        if (StringUtils.isNotBlank(likedVlogCountsStr)) {
//            likedVlogCounts = Integer.valueOf(likedVlogCountsStr);
//        }
                if (StringUtils.isNotBlank(likedVlogerCountsStr)) {
                        likedVlogerCounts = Integer.valueOf(likedVlogerCountsStr);
                }
                totalLikeMeCounts = likedVlogCounts + likedVlogerCounts;

                usersVO.setMyFollowsCounts(myFollowsCounts);
                usersVO.setMyFansCounts(myFansCounts);
                usersVO.setTotalLikeMeCounts(totalLikeMeCounts);

                return GraceJSONResult.ok(usersVO);
        }
        @PostMapping("modifyUserInfo")
        public GraceJSONResult modifyUserInfo(@RequestBody UserInfoBo userInfoBo,@RequestParam Integer type ){
                UserInfoModifyType.checkUserInfoTypeIsRight(type);
                Users userInfo = usersService.updatedUserInfo(userInfoBo, type);
               return   GraceJSONResult.ok(userInfo);
        }
        @PostMapping("modifyImage")
        public GraceJSONResult modifyImage(MultipartFile file,@RequestParam String userId ,@RequestParam Integer type ) throws  Exception{
                //判断上传参数
              if (FileTypeEnum.BGIMG.type != type && FileTypeEnum.FACE.type != type){
                      GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_FAILD);
              }
              //上传文件到minio
                String filename = file.getOriginalFilename();
                MinIOUtils.uploadFile(minIOConfig.getBucketName(),filename,file.getInputStream());
                String imgUrl = minIOConfig.getFileHost() + "/" + minIOConfig.getBucketName() + "/" + filename;

                //设置数据库的值  修改图片地址
                UserInfoBo userInfoBo = new UserInfoBo();
                userInfoBo.setId(userId);
                if (FileTypeEnum.BGIMG.type == type ){
                        userInfoBo.setBgImg(imgUrl);
                }else {
                        userInfoBo.setFace(imgUrl);
                }
                Users user = usersService.updatedUserInfo(userInfoBo);
                return GraceJSONResult.ok(user);

        }

}
